package com.webinsky.article.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webinsky.article.domain.MisRelationship;
import com.webinsky.article.domain.MisRelationshipSync;
import com.webinsky.article.domain.MisRelationshipType;
import com.webinsky.article.persist.dao.ArticleMapper;
import com.webinsky.article.persist.dao.ArticleRelateMapper;
import com.webinsky.article.persist.dao.ArticleRelateVideoMapper;
import com.webinsky.article.persist.dao.ArticleRelationshipMapper;
import com.webinsky.article.persist.dao.ArticleTagMapper;
import com.webinsky.article.persist.entity.Article;
import com.webinsky.article.persist.entity.ArticleRelate;
import com.webinsky.article.persist.entity.ArticleRelateVideo;
import com.webinsky.article.persist.entity.ArticleRelationship;
import com.webinsky.article.persist.entity.ArticleTag;
import com.webinsky.article.service.RelationshipManagement;
import com.webinsky.common.proj.DelFlag;
import com.webinsky.common.proj.Lang;

@Service
public class RelationshipManagementImpl implements RelationshipManagement {
	// logger
	private static final Logger logger = LoggerFactory.getLogger(RelationshipManagementImpl.class);
	
	@Autowired
	private ArticleRelationshipMapper articleRelationDao;
	@Autowired
	private ArticleTagMapper articleTagDao;
	@Autowired
	private ArticleMapper articleDao;
	@Autowired
	private ArticleRelateMapper articleRelateDao;
	@Autowired
	private ArticleRelateVideoMapper relateVideoDao;
	
	
	private void syncRelationVideo(MisRelationshipSync relationSync){
		List<Integer> cnArticleIds = new ArrayList<>();
		relationSync.getCnArticles().forEach( (artileName, articleId) -> {
			cnArticleIds.add(articleId);
		});
		if ( cnArticleIds.isEmpty() ) {
			return;
		}
		List<ArticleRelateVideo> relateCnVideos = relateVideoDao.selectByArticleIds(cnArticleIds);
//		if ( relateCnVideos.isEmpty() ){
//			return;
//		}
		// articleId -> relativeVideos
		Map<Integer, List<ArticleRelateVideo>> relateCnArticleVideosMap = relateCnVideos.stream().collect(Collectors.groupingBy(ArticleRelateVideo::getArticleId));
		// articleId -> relativeVideoId
		Map<Integer, List<Integer>> relateCnVideosMap = new HashMap<>();
		relateCnVideos.forEach(rv -> {
			if ( relateCnVideosMap.get(rv.getArticleId()) == null ){
				relateCnVideosMap.put(rv.getArticleId(), new ArrayList<>());
			}
			relateCnVideosMap.get(rv.getArticleId()).add(rv.getRelateVideoId());
		});
		// video id -> video name
		Map<Integer, String> cnVideoId2Name = new HashMap<>();
		relationSync.getCnRelateVideo().forEach( (name, id) -> {
			cnVideoId2Name.put(id,  name);
		});
		// articleId -> relativeVideoName
		Map<Integer, List<String>> relativeCnVideosNameMap = new HashMap<>();
		relateCnVideosMap.forEach((cnArticleId, relativeCnVideoIds) -> {			
			relativeCnVideoIds.forEach(relativeCnVideoId -> {
				if ( relativeCnVideosNameMap.get(cnArticleId) == null ){
					relativeCnVideosNameMap.put(cnArticleId, new ArrayList<>());
				}
				relativeCnVideosNameMap.get(cnArticleId).add(cnVideoId2Name.get(relativeCnVideoId));
			});
		});
		
		List<ArticleRelateVideo> existsRelateVideos = relateVideoDao.selectActiveAll();
		Map<Integer, Integer> existsRelateVideoMap = new HashMap<>();
		existsRelateVideos.forEach( v -> {
			existsRelateVideoMap.put(v.getArticleId(), v.getRelateVideoId());
		});
		// 日文
		relationSync.getCnArticles().forEach((cnName, id) -> {
			if ( relationSync.getJpArticles().get(cnName) != null ){
				// delete jp relate videos
				relateVideoDao.deleteByArticleId(relationSync.getJpArticles().get(cnName));
				// 取得viodeIds
				List<String> cnVideoNames = relativeCnVideosNameMap.get(id);
				if ( cnVideoNames != null && !cnVideoNames.isEmpty() ){
					cnVideoNames.forEach(cnVideoName -> {
						if ( relationSync.getJpRelateVideo().get(cnVideoName) != null ){
							// 取得sort
							int sort = 0;
							Integer cnVideoId = relationSync.getCnRelateVideo().get(cnVideoName);
							for ( ArticleRelateVideo av : relateCnArticleVideosMap.get(id) ){
								if ( av.getRelateVideoId() == cnVideoId ){
									sort = av.getSort();
									break;
								}
							}
							// 保存
							ArticleRelateVideo record = new ArticleRelateVideo();
							record.setArticleId(relationSync.getJpArticles().get(cnName));
							record.setDel(DelFlag.UNDel.getFlag());
							record.setLanguage(Lang.jaJP.getLang());
							record.setRelateVideoId(relationSync.getJpRelateVideo().get(cnVideoName));
							record.setSort(sort);
							relateVideoDao.insert(record);
						}
					});
				}
			}
		});
		// 英文
		relationSync.getCnArticles().forEach((cnName, id) -> {
			if ( relationSync.getEnArticles().get(cnName) != null ){
				// delete en relate videos
				relateVideoDao.deleteByArticleId(relationSync.getEnArticles().get(cnName));
				// 取得viodeIds
				List<String> cnVideoNames = relativeCnVideosNameMap.get(id);
				if ( cnVideoNames != null && !cnVideoNames.isEmpty() ){
					cnVideoNames.forEach(cnVideoName -> {
						if ( relationSync.getEnRelateVideo().get(cnVideoName) != null ){
							// 取得sort
							int sort = 0;
							Integer cnVideoId = relationSync.getCnRelateVideo().get(cnVideoName);
							for ( ArticleRelateVideo av : relateCnArticleVideosMap.get(id) ){
								if ( av.getRelateVideoId() == cnVideoId ){
									sort = av.getSort();
									break;
								}
							}
							// 保存
							ArticleRelateVideo record = new ArticleRelateVideo();
							record.setArticleId(relationSync.getEnArticles().get(cnName));
							record.setDel(DelFlag.UNDel.getFlag());
							record.setLanguage(Lang.enUS.getLang());
							record.setRelateVideoId(relationSync.getEnRelateVideo().get(cnVideoName));
							record.setSort(sort);
							relateVideoDao.insert(record);
						}
					});
				}
			}
		});
		// 台湾
		relationSync.getCnArticles().forEach((cnName, id) -> {
			if ( relationSync.getTwArticles().get(cnName) != null ){
				// delete tw relate videos
				relateVideoDao.deleteByArticleId(relationSync.getTwArticles().get(cnName));
				// 取得viodeIds
				List<String> cnVideoNames = relativeCnVideosNameMap.get(id);
				if ( cnVideoNames != null && !cnVideoNames.isEmpty() ){
					cnVideoNames.forEach(cnVideoName -> {
						if ( relationSync.getTwRelateVideo().get(cnVideoName) != null ){
							// 取得sort
							int sort = 0;
							Integer cnVideoId = relationSync.getCnRelateVideo().get(cnVideoName);
							for ( ArticleRelateVideo av : relateCnArticleVideosMap.get(id) ){
								if ( av.getRelateVideoId() == cnVideoId ){
									sort = av.getSort();
									break;
								}
							}
							// 保存
							ArticleRelateVideo record = new ArticleRelateVideo();
							record.setArticleId(relationSync.getTwArticles().get(cnName));
							record.setDel(DelFlag.UNDel.getFlag());
							record.setLanguage(Lang.zhTW.getLang());
							record.setRelateVideoId(relationSync.getTwRelateVideo().get(cnVideoName));
							record.setSort(sort);
							relateVideoDao.insert(record);
						}
					});
				}
			}
		});
		// 泰语
		relationSync.getCnArticles().forEach((cnName, id) -> {
			if ( relationSync.getThArticles().get(cnName) != null ){
				// delete tw relate videos
				relateVideoDao.deleteByArticleId(relationSync.getThArticles().get(cnName));
				// 取得viodeIds
				List<String> cnVideoNames = relativeCnVideosNameMap.get(id);
				if ( cnVideoNames != null && !cnVideoNames.isEmpty() ){
					cnVideoNames.forEach(cnVideoName -> {
						if ( relationSync.getThRelateVideo().get(cnVideoName) != null ){
							// 取得sort
							int sort = 0;
							Integer cnVideoId = relationSync.getCnRelateVideo().get(cnVideoName);
							for ( ArticleRelateVideo av : relateCnArticleVideosMap.get(id) ){
								if ( av.getRelateVideoId() == cnVideoId ){
									sort = av.getSort();
									break;
								}
							}
							// 保存
							ArticleRelateVideo record = new ArticleRelateVideo();
							record.setArticleId(relationSync.getThArticles().get(cnName));
							record.setDel(DelFlag.UNDel.getFlag());
							record.setLanguage(Lang.th.getLang());
							record.setRelateVideoId(relationSync.getThRelateVideo().get(cnVideoName));
							record.setSort(sort);
							relateVideoDao.insert(record);
						}
					});
				}
			}
		});
		// 韩文
		relationSync.getCnArticles().forEach((cnName, id) -> {
			if ( relationSync.getKrArticles().get(cnName) != null ){
				// delete tw relate videos
				relateVideoDao.deleteByArticleId(relationSync.getKrArticles().get(cnName));
				// 取得viodeIds
				List<String> cnVideoNames = relativeCnVideosNameMap.get(id);
				if ( cnVideoNames != null && !cnVideoNames.isEmpty() ){
					cnVideoNames.forEach(cnVideoName -> {
						if ( relationSync.getKrRelateVideo().get(cnVideoName) != null ){
							// 取得sort
							int sort = 0;
							Integer cnVideoId = relationSync.getCnRelateVideo().get(cnVideoName);
							for ( ArticleRelateVideo av : relateCnArticleVideosMap.get(id) ){
								if ( av.getRelateVideoId() == cnVideoId ){
									sort = av.getSort();
									break;
								}
							}
							// 保存
							ArticleRelateVideo record = new ArticleRelateVideo();
							record.setArticleId(relationSync.getKrArticles().get(cnName));
							record.setDel(DelFlag.UNDel.getFlag());
							record.setLanguage(Lang.th.getLang());
							record.setRelateVideoId(relationSync.getKrRelateVideo().get(cnVideoName));
							record.setSort(sort);
							relateVideoDao.insert(record);
						}
					});
				}
			}
		});		
		
	}
	
	
	private void syncRelationArticle(MisRelationshipSync relationSync) {
		List<Integer> cnArticleIds = new ArrayList<>();
		relationSync.getCnArticles().forEach( (artileName, articleId) -> {
			cnArticleIds.add(articleId);
		});
		if ( cnArticleIds.isEmpty() ) {
			return;
		}
		List<ArticleRelate> articleRelates = articleRelateDao.selectByArticleIds(cnArticleIds);
		if ( articleRelates.isEmpty() ) {
			return;
		}
		Map<Integer, List<ArticleRelate>> relateMap = articleRelates.stream().collect(Collectors.groupingBy(ArticleRelate::getArticleId));
		if ( relateMap.isEmpty() ) {
			return;
		}
		Map<Integer, String> cnArticleId2Name = new HashMap<>();
		Map<String, Integer> cnArticleName2Id = new HashMap<>();
		List<Article> cnArticleInfos = articleDao.selectActiveBySiteIdAndLang(relationSync.getSiteId(), Lang.zhCN.getLang());
		cnArticleInfos.forEach( cnArticleInfo ->{
			cnArticleId2Name.put(cnArticleInfo.getId(), cnArticleInfo.getArticleNo());
			cnArticleName2Id.put(cnArticleInfo.getArticleNo(), cnArticleInfo.getId());
		});
		// 转换成name -> List<> names
		Map<String, List<String>> cnNamesMap = new HashMap<>();
		relateMap.forEach( (cnArticleId, articles) -> {
			if ( cnNamesMap.get(cnArticleId2Name.get(cnArticleId)) == null ){
				cnNamesMap.put(cnArticleId2Name.get(cnArticleId), new ArrayList<>());
			}
			articles.forEach( e -> {
				cnNamesMap.get(cnArticleId2Name.get(cnArticleId)).add(cnArticleId2Name.get(e.getRelateArticleId()));
			});
		});
		// 处理日文
		cnNamesMap.forEach( (strName, strNames) -> {
			Integer jaId = relationSync.getJpArticles().get(strName);
			if ( jaId != null ) {
				// delete relate article
				articleRelateDao.deleteByArticleId(jaId);
				Integer cnarticleId = cnArticleName2Id.get(strName);
				List<ArticleRelate> jaArtileRelates = articleRelateDao.selectByArticleId(jaId, DelFlag.UNDel.getFlag());
				strNames.forEach(re -> {
					Integer cnrelateArticleId = cnArticleName2Id.get(re);
					Integer sort = 0;
					for ( ArticleRelate artRel : articleRelates ){
						if ( artRel.getArticleId() == cnarticleId && artRel.getRelateArticleId() == cnrelateArticleId ){
							sort = artRel.getSort();
						}
					}
					
					if ( !jaArtileRelates.isEmpty() ) {
						for ( ArticleRelate are : jaArtileRelates ) {
							if ( are.getRelateArticleId() == relationSync.getJpArticles().get(re) && are.getArticleId() == jaId) {
								are.setSort(sort);
								articleRelateDao.updateByPrimaryKey(are);
								return;
							}
						}
					}
					Integer jaRelateId = relationSync.getJpArticles().get(re);
					if ( jaRelateId != null ) {
						ArticleRelate articleRelate = new ArticleRelate();
						articleRelate.setArticleId(jaId);
						articleRelate.setSort(sort);
						articleRelate.setDel(DelFlag.UNDel.getFlag());
						articleRelate.setLanguage(Lang.jaJP.getLang());
						articleRelate.setRelateArticleId(relationSync.getJpArticles().get(re));
						articleRelateDao.insert(articleRelate);
					}
				});
			}
		});
		// 处理英文
		cnNamesMap.forEach( (strName, strNames) -> {
			Integer enId = relationSync.getEnArticles().get(strName);
			if ( enId != null ) {
				// delete relate article
				articleRelateDao.deleteByArticleId(enId);
				Integer cnarticleId = cnArticleName2Id.get(strName);
				List<ArticleRelate> enArtileRelates = articleRelateDao.selectByArticleId(enId, DelFlag.UNDel.getFlag());
				strNames.forEach(re -> {
					Integer cnrelateArticleId = cnArticleName2Id.get(re);
					Integer sort = 0;
					for ( ArticleRelate artRel : articleRelates ){
						if ( artRel.getArticleId() == cnarticleId && artRel.getRelateArticleId() == cnrelateArticleId ){
							
							sort = artRel.getSort();
						}
					}
					
					if ( !enArtileRelates.isEmpty() ) {
						for ( ArticleRelate are : enArtileRelates ) {
							if ( are.getRelateArticleId() == relationSync.getEnArticles().get(re) && are.getArticleId() == enId) {
								are.setSort(sort);
								articleRelateDao.updateByPrimaryKey(are);
								return;
							}
						}
					}
					Integer enRelateId = relationSync.getEnArticles().get(re);
					if ( enRelateId != null ) {
						ArticleRelate articleRelate = new ArticleRelate();
						articleRelate.setArticleId(enId);
						articleRelate.setSort(sort);
						articleRelate.setDel(DelFlag.UNDel.getFlag());
						articleRelate.setLanguage(Lang.enUS.getLang());
						articleRelate.setRelateArticleId(relationSync.getEnArticles().get(re));
						articleRelateDao.insert(articleRelate);
					}
				});
			}
		});
		// 处理繁体
		cnNamesMap.forEach( (strName, strNames) -> {
			Integer twId = relationSync.getTwArticles().get(strName);
			if ( twId != null ) {
				// delete relate article
				articleRelateDao.deleteByArticleId(twId);
				Integer cnarticleId = cnArticleName2Id.get(strName);
				List<ArticleRelate> twArtileRelates = articleRelateDao.selectByArticleId(twId, DelFlag.UNDel.getFlag());
				strNames.forEach(re -> {
					Integer cnrelateArticleId = cnArticleName2Id.get(re);
					Integer sort = 0;
					for ( ArticleRelate artRel : articleRelates ){
						if ( artRel.getArticleId() == cnarticleId && artRel.getRelateArticleId() == cnrelateArticleId ){
							
							sort = artRel.getSort();
						}
					}
					
					if ( !twArtileRelates.isEmpty() ) {
						for ( ArticleRelate are : twArtileRelates ) {
							if ( are.getRelateArticleId() == relationSync.getTwArticles().get(re) && are.getArticleId() == twId) {
								are.setSort(sort);
								articleRelateDao.updateByPrimaryKey(are);
								return;
							}
						}
					}
					Integer twRelateId = relationSync.getTwArticles().get(re);
					if ( twRelateId != null ) {
						ArticleRelate articleRelate = new ArticleRelate();
						articleRelate.setArticleId(twId);
						articleRelate.setSort(sort);
						articleRelate.setDel(DelFlag.UNDel.getFlag());
						articleRelate.setLanguage(Lang.zhTW.getLang());
						articleRelate.setRelateArticleId(relationSync.getTwArticles().get(re));
						articleRelateDao.insert(articleRelate);
					}
				});
			}
		});
		// 处理韩文
		cnNamesMap.forEach( (strName, strNames) -> {
			Integer krId = relationSync.getKrArticles().get(strName);
			if ( krId != null ) {
				// delete relate article
				articleRelateDao.deleteByArticleId(krId);
				Integer cnarticleId = cnArticleName2Id.get(strName);
				List<ArticleRelate> krArtileRelates = articleRelateDao.selectByArticleId(krId, DelFlag.UNDel.getFlag());
				strNames.forEach(re -> {
					Integer cnrelateArticleId = cnArticleName2Id.get(re);
					Integer sort = 0;
					for ( ArticleRelate artRel : articleRelates ){
						if ( artRel.getArticleId() == cnarticleId && artRel.getRelateArticleId() == cnrelateArticleId ){
							sort = artRel.getSort();
						}
					}
					
					if ( !krArtileRelates.isEmpty() ) {
						for ( ArticleRelate are : krArtileRelates ) {
							if ( are.getRelateArticleId() == relationSync.getKrArticles().get(re) && are.getArticleId() == krId) {
								are.setSort(sort);
								articleRelateDao.updateByPrimaryKey(are);
								return;
							}
						}
					}
					Integer krRelateId = relationSync.getKrArticles().get(re);
					if ( krRelateId != null ) {
						ArticleRelate articleRelate = new ArticleRelate();
						articleRelate.setArticleId(krId);
						articleRelate.setSort(sort);
						articleRelate.setDel(DelFlag.UNDel.getFlag());
						articleRelate.setLanguage(Lang.ko.getLang());
						articleRelate.setRelateArticleId(relationSync.getKrArticles().get(re));
						articleRelateDao.insert(articleRelate);
					}
				});
			}
		});
		// 处理泰文
		cnNamesMap.forEach( (strName, strNames) -> {
			Integer thId = relationSync.getThArticles().get(strName);
			if ( thId != null ) {
				// delete relate article
				articleRelateDao.deleteByArticleId(thId);
				Integer cnarticleId = cnArticleName2Id.get(strName);
				List<ArticleRelate> thArtileRelates = articleRelateDao.selectByArticleId(thId, DelFlag.UNDel.getFlag());
				strNames.forEach(re -> {
					Integer cnrelateArticleId = cnArticleName2Id.get(re);
					Integer sort = 0;
					for ( ArticleRelate artRel : articleRelates ){
						if ( artRel.getArticleId() == cnarticleId && artRel.getRelateArticleId() == cnrelateArticleId ){
							sort = artRel.getSort();
						}
					}
					if ( !thArtileRelates.isEmpty() ) {
						for ( ArticleRelate are : thArtileRelates ) {
							if ( are.getRelateArticleId() == relationSync.getThArticles().get(re) && are.getArticleId() == thId) {
								are.setSort(sort);
								articleRelateDao.updateByPrimaryKey(are);
								return;
							}
						}
					}
					Integer thRelateId = relationSync.getThArticles().get(re);
					if ( thRelateId != null ) {
						ArticleRelate articleRelate = new ArticleRelate();
						articleRelate.setArticleId(thId);
						articleRelate.setSort(sort);
						articleRelate.setDel(DelFlag.UNDel.getFlag());
						articleRelate.setLanguage(Lang.ko.getLang());
						articleRelate.setRelateArticleId(relationSync.getThArticles().get(re));
						articleRelateDao.insert(articleRelate);
					}
				});
			}
		});		
		
	}
		
	private void syncTag(MisRelationshipSync relationSync, List<ArticleRelationship> ships) {
		List<Integer> cnArticleIds = new ArrayList<>();
		Map<Integer, String> cnId2Name = new HashMap<>();
		for ( String artileNo :	relationSync.getCnArticles().keySet() ){
			cnArticleIds.add(relationSync.getCnArticles().get(artileNo));
			cnId2Name.put(relationSync.getCnArticles().get(artileNo), artileNo);
		}
		Map<Integer, ArticleRelationship> cnShips = ships.stream().collect(Collectors.toMap(ArticleRelationship::getCnId, Function.identity()));
		List<ArticleTag> cnArticleTags = articleTagDao.selectByActiveByArticleIds(cnArticleIds);
		Map<Integer, List<ArticleTag>> cnArticleTagMap = null;
		if ( cnArticleTags.isEmpty() ){
			cnArticleTagMap = new HashMap<>();
			for ( Integer cnId : cnArticleIds ){
				cnArticleTagMap.put(cnId, new ArrayList<>());
			}
		} else {
			cnArticleTagMap = cnArticleTags.stream().collect(Collectors.groupingBy(ArticleTag::getArticleId));			
		}
		cnArticleTagMap.forEach((cnArtileId, articleTags) -> {
			// 取得日文, 英文, 泰文, 繁体, 韩文的文章ID
			String cnArticleNo = cnId2Name.get(cnArtileId);
			Integer jpArticleId = relationSync.getJpArticles().get(cnArticleNo);
			Integer enArticleId = relationSync.getEnArticles().get(cnArticleNo);
			Integer thArticleId = relationSync.getThArticles().get(cnArticleNo);
			Integer zhTwArticleId = relationSync.getTwArticles().get(cnArticleNo);
			Integer krArticleId = relationSync.getKrArticles().get(cnArticleNo);
			// 删除文章的tag
			articleTagDao.deleteByArticleId(jpArticleId);
			articleTagDao.deleteByArticleId(enArticleId);
			articleTagDao.deleteByArticleId(thArticleId);
			articleTagDao.deleteByArticleId(zhTwArticleId);
			articleTagDao.deleteByArticleId(krArticleId);
			// 新建tag 取得对应的日文, 英文, 泰文, 繁体, 韩文
			articleTags.forEach(articleTag -> {
				Integer articleTagId = articleTag.getTagId();
				if ( cnShips.get(articleTagId) != null ){
					Integer jpTagId = cnShips.get(articleTagId).getJpId();
					Integer enTagId = cnShips.get(articleTagId).getEnId();
					Integer krTagId = cnShips.get(articleTagId).getKrId();
					Integer thTagId = cnShips.get(articleTagId).getThId();
					Integer twTagId = cnShips.get(articleTagId).getTwId();
					ArticleTag jptag = new ArticleTag();
					jptag.setArticleId(jpArticleId);
					jptag.setDel(DelFlag.UNDel.getFlag());
					jptag.setLanguage(Lang.jaJP.getLang());
					jptag.setTagId(jpTagId);
					articleTagDao.insert(jptag);
					ArticleTag enTag = new ArticleTag();
					enTag.setArticleId(enArticleId);
					enTag.setDel(DelFlag.UNDel.getFlag());
					enTag.setLanguage(Lang.enUS.getLang());
					enTag.setTagId(enTagId);
					articleTagDao.insert(enTag);
					ArticleTag krTag = new ArticleTag();
					krTag.setArticleId(krArticleId);
					krTag.setDel(DelFlag.UNDel.getFlag());
					krTag.setLanguage(Lang.ko.getLang());
					krTag.setTagId(krTagId);
					articleTagDao.insert(krTag);
					ArticleTag thTag = new ArticleTag();
					thTag.setArticleId(thArticleId);
					thTag.setDel(DelFlag.UNDel.getFlag());
					thTag.setLanguage(Lang.th.getLang());
					thTag.setTagId(thTagId);
					articleTagDao.insert(thTag);
					ArticleTag twTag = new ArticleTag();
					twTag.setArticleId(zhTwArticleId);
					twTag.setDel(DelFlag.UNDel.getFlag());
					twTag.setLanguage(Lang.zhTW.getLang());
					twTag.setTagId(twTagId);
					articleTagDao.insert(twTag);
				}
			});
		});
	}
	
	private ArticleRelationship convert(MisRelationship misRelationship) {
		ArticleRelationship relationShip = new ArticleRelationship();
		relationShip.setSiteId(misRelationship.getSiteId());
		relationShip.setCnId(misRelationship.getCnId());
		relationShip.setEnId(misRelationship.getEnId());
		relationShip.setJpId(misRelationship.getJpId());
		relationShip.setKrId(misRelationship.getKrId());
		relationShip.setThId(misRelationship.getThId());
		relationShip.setTwId(misRelationship.getTwId());
		relationShip.setType(misRelationship.getType().getType());
		return relationShip;
	}
	
	private MisRelationship convert(ArticleRelationship ship) {
		MisRelationship misRelationship = new MisRelationship();
		misRelationship.setSiteId(ship.getSiteId());
		misRelationship.setCnId(ship.getCnId());
		misRelationship.setEnId(ship.getEnId());
		misRelationship.setJpId(ship.getJpId());
		misRelationship.setId(ship.getId());
		misRelationship.setKrId(ship.getKrId());
		misRelationship.setThId(ship.getThId());
		misRelationship.setTwId(ship.getTwId());
		misRelationship.setType(MisRelationshipType.parse(ship.getType()));
		return misRelationship;
	}
	
	private ArticleRelationship convert(ArticleRelationship ship, MisRelationship misRelationship){
		ship.setSiteId(misRelationship.getSiteId());
		ship.setCnId(misRelationship.getCnId());
		ship.setJpId(misRelationship.getJpId());
		ship.setKrId(misRelationship.getKrId());
		ship.setThId(misRelationship.getThId());
		ship.setTwId(misRelationship.getTwId());
		ship.setType(misRelationship.getType().getType());
		return ship;
	}
	
	@Override
	public void create(MisRelationship misRelationship) {
		if ( misRelationship != null ) {
			ArticleRelationship ship = convert(misRelationship);
			articleRelationDao.insert(ship);
			misRelationship.setId(ship.getId());
		}
	}

	@Override
	public List<MisRelationship> getRelationshipBySiteIdAndType(int siteId, MisRelationshipType type) {
		if ( siteId > 0 && type != null ) {
			List<ArticleRelationship> ships = articleRelationDao.selectBySiteIdAndType(siteId, type.getType());
			if ( !ships.isEmpty() ) {
				List<MisRelationship> misShips = new ArrayList<>();
				ships.forEach(s -> misShips.add(convert(s)));
				return misShips;
			}
		}
		return new ArrayList<>();
	}

	@Override
	public void syncRelationship(MisRelationshipSync relationSync) {
		if ( relationSync == null || 
				relationSync.getSiteId() > 0 || 
					relationSync.getSyncItems().isEmpty() ){
			logger.info("{} syncRelationship relationSync = null");
			return;
		}
		// 取得关联关系
		for ( MisRelationshipType misRelShipType : relationSync.getSyncItems() ) {
			switch ( misRelShipType ) {
			case Tag: {
				List<ArticleRelationship> relationships = articleRelationDao.selectBySiteIdAndType(relationSync.getSiteId(), MisRelationshipType.Tag.getType());
				syncTag(relationSync, relationships);
//				Map<String, List<ArticleRelationship>> relationshipMap = relationships.stream().collect(Collectors.groupingBy(ArticleRelationship::getType));
//				relationshipMap.forEach((relationshipType, artilcleShips) -> {
//					MisRelationshipType misType = MisRelationshipType.parse(relationshipType);
//					if ( misType != null && misType == MisRelationshipType.Tag ) {
//						syncTag(relationSync, artilcleShips);
//					}
//				});
				break;
			}
			case RelateArticle:
				syncRelationArticle(relationSync);
				break;
			case RelateVideo:
				syncRelationVideo(relationSync);
				break;
			default:
			}
		}
		
		
	}

	@Override
	public List<MisRelationship> getRelationshipByCnIds(List<Integer> cnIds) {
		if ( cnIds == null || cnIds.isEmpty() ){
			return new ArrayList<>();
		}
		List<ArticleRelationship> ships = articleRelationDao.selectByCnIds(cnIds);
		if ( ships.isEmpty() ){
			return new ArrayList<>();
		}
		List<MisRelationship> misRelationships = new ArrayList<>();
		ships.forEach( e -> {
			misRelationships.add(convert(e));
		});
		return misRelationships;
	}

}
